#include "stdafx.h"
#include "VS2010CtxMenuFixApp.h"

// Forward declarations of functions included in this code module:
static LRESULT TrayMessageHandler(HWND, UINT, WPARAM, LPARAM);
static LRESULT MenuMessageHandler(HWND, UINT, WPARAM, LPARAM);

LRESULT CALLBACK AppWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_APP_TRAYCALLBACK:
        return TrayMessageHandler(hWnd, msg, wParam, lParam);

    case WM_COMMAND:
        return MenuMessageHandler(hWnd, msg, wParam, lParam);

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    return 0;
}

static LRESULT TrayMessageHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (lParam)
    {
    case WM_RBUTTONDOWN:
    case WM_CONTEXTMENU:
        POINT pt;
        GetCursorPos(&pt);
        TrackPopupMenu(g_cAppResources->GetTrayMenu(), TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, NULL);
        break;

    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    return 0;
}

static LRESULT MenuMessageHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int wmId = LOWORD(wParam);

    switch (wmId)
    {
    case IDM_ABOUT:
        DialogBox(g_hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, AboutDlgProc);
        break;

    case IDM_EXIT:
        DestroyWindow(hWnd);
        break;

    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    return 0;
}
